package com.thunbu.thunbu_meeting_system.activity

import androidx.recyclerview.widget.LinearLayoutManager
import com.cathu.lib_commonkt.base.BaseMVVMActivity
import com.hjq.toast.ToastUtils
import com.thunbu.lib_common.utils.TimeUtil
import com.thunbu.lib_common.widget.CommonToolBar
import com.thunbu.thunbu_meeting_system.BaseVBQuickAdapter
import com.thunbu.thunbu_meeting_system.bean.ScanRegisterWapperModel
import com.thunbu.thunbu_meeting_system.bindAdapter
import com.thunbu.thunbu_meeting_system.databinding.ActivityScanRegisterBinding
import com.thunbu.thunbu_meeting_system.databinding.ItemInventoryListItemOtherBinding
import com.thunbu.thunbu_meeting_system.gotoActivity
import com.thunbu.thunbu_meeting_system.ternary
import com.thunbu.thunbu_meeting_system.viewMdel.ScanRegisterVM
/**
 * @Description: 扫码登记
 * @author: Yang.Yang33
 * @Date: 2023/3/23 20:28
 */
class ScanRegisterActivity:BaseMVVMActivity<ActivityScanRegisterBinding,ScanRegisterVM>() {
    lateinit var toolBar: CommonToolBar
    private lateinit var mAdapter: BaseVBQuickAdapter<ScanRegisterWapperModel.Page.Record, ItemInventoryListItemOtherBinding>
    val ORIGINAL_PAGE = 1
    var mPage = ORIGINAL_PAGE
    private var checkLeft = true


    override fun onMVVMCreated() {
        initToolBar()
        initRv()
        initView()
        initVM()
        loadData()
    }

    private fun initVM() {
        vm.scanRegisterModel.observe(this){
            bindViews.refereshLayout.finishLoadMore()
            if (mPage == ORIGINAL_PAGE) {
                mAdapter.setNewData(it.page?.records)
            } else {
                mAdapter.addData(it.page?.records?: mutableListOf())
            }
            if(it.page?.total?:0 > mAdapter.itemCount+1){
                mPage++
            }else{
                bindViews.refereshLayout.setEnableLoadMore(false)
            }
            bindViews.tvCodeCount.text = it.inventoryTaskPO?.labelCount.toString()
            bindViews.tvRegisterCount.text = it.inventoryTaskPO?.scannedCodeCount.toString()
            bindViews.tvCancelRegisterCount.text = it.inventoryTaskPO?.revocationRegister.toString()
            bindViews.tvStatus.text = when(it.inventoryTaskPO?.taskStatus){
                "1" -> "未完成"
                "2" -> "已结束"
                "3" -> "未开始"
                else -> "其他"
            }
            bindViews.tvTime.text = it.inventoryTaskPO?.creatTime
        }
    }

    private fun loadData(){
        vm.loadData(checkLeft.ternary(1,2),bindViews.searchBar.etSearch.text?.toString(),mPage,20)
    }

    private fun initView() {
        setSelected()
        bindViews.refereshLayout.setEnableRefresh(false)
        bindViews.refereshLayout.setEnableLoadMoreWhenContentNotFull(false)
        bindViews.refereshLayout.setEnableLoadMore(true)
        bindViews.refereshLayout.setOnLoadMoreListener {
            loadData()
        }
        bindViews.searchBar.ivSearch.setOnClickListener {
            mPage = 0
            loadData()
        }
        bindViews.rlLeft.setOnClickListener {
            checkLeft = !checkLeft
            setSelected()
            mPage = 0
            loadData()
        }
        bindViews.rlRight.setOnClickListener {
            checkLeft = !checkLeft
            setSelected()
            mPage = 0
            loadData()
        }

    }

    private fun setSelected(){
        bindViews.rlLeft.isSelected = checkLeft
        bindViews.tvRegisterCount.isSelected = checkLeft
        bindViews.tvRegisterCountDes.isSelected = checkLeft
        bindViews.rlRight.isSelected = !checkLeft
        bindViews.tvCancelRegisterCount.isSelected = !checkLeft
        bindViews.tvCancelRegisterCountDes.isSelected = !checkLeft
    }

    private fun initToolBar() {
        toolBar = setToolBar(CommonToolBar::class.java)
        toolBar.setBackAction { finish() }
        toolBar.setTitle("扫码登记")
    }

    private fun initRv() {
        bindViews.rv.layoutManager = LinearLayoutManager(this)
        mAdapter = bindViews.rv.bindAdapter { item, position ->
            itemInclude.tvName.text = mAdapter.data[position].assetsName
            itemInclude.tvNumber.text = mAdapter.data[position].inventoryCode
            itemInclude.tvType.text = mAdapter.data[position].typeName
            itemInclude.tvAdr.text = mAdapter.data[position].place
        }
        mAdapter.setOnItemClickListener { adapter, view, position ->
            gotoActivity<InventoryDetailActivity>{
                putExtra(InventoryDetailActivity.ID,mAdapter.data[position].id)
            }
        }

    }
}